import { PlusOutlined } from '@ant-design/icons';
import { Button, message, Popconfirm } from 'antd';
import { history } from 'umi';
import { useState } from 'react';
import React, { useRef } from 'react';
import { PageContainer } from '@ant-design/pro-layout';
import type { ProColumns, ActionType } from '@ant-design/pro-table';
import ProTable from '@ant-design/pro-table';
import { list, updateStatus, update } from './service';
import type { TableListItem, TableListPagination } from './data';

const TableList: React.FC = () => {
  const actionRef = useRef<ActionType>();
  const [formData, setFormData] = useState<API.RuleListItem>({});

  const handleStop = async (record: API.RuleListItem[]) => {
    const hide = message.loading('正在处理');
    let item = { id: record.id };
    item.status = record.status == 1 ? 2 : 1;
    try {
      const response = await updateStatus(item);
      if (response.code == 200) {
        message.success('处理成功,即将刷新');
      } else {
        message.error('处理失败!');
      }
      hide();
      actionRef.current?.reloadAndRest?.();
    } catch (error) {
      hide();
      message.error('处理失败');
      return false;
    }
  };

  const columns: ProColumns<TableListItem>[] = [
    {
      title: '活动ID',
      dataIndex: 'id',
      width: '100px',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '活动标题',
      dataIndex: 'title',
      hideInForm: true,
      hideInSearch: true,
      width: '150px',
    },
    {
      title: '活动类型',
      dataIndex: 'type',
      hideInForm: true,
      hideInSearch: true,
      width: '150px',
      valueType: 'select',
      valueEnum: {
        1: { text: '病历大赛' },
      },
    },
    {
      title: '活动状态',
      dataIndex: 'status',
      width: '150px',
      valueType: 'select',
      valueEnum: {
        0: { text: '未开始' },
        1: {
          text: '进行中',
        },
        2: {
          text: '暂停',
        },
        3: {
          text: '已结束',
        },
      },
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      width: '150px',
      valueType: 'date',
      hideInForm: true,
      hideInSearch: true,
    },
    {
      title: '活动有效期',
      dataIndex: 'startTime',
      hideInForm: true,
      hideInSearch: true,
      width: '150px',
      render: (text, record, index) => {
        return record.startTime + '-' + record.endTime;
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      width: '150px',
      render: (_, record) => [
        <a
          key="eidt"
          onClick={() => {
            history.push('/activity/activity-detail?id=' + record.id);
          }}
        >
          编辑
        </a>,
        record.status == 1 ? (
          <Popconfirm key="top" title="确定要暂停活动吗?" onConfirm={() => handleStop(record)}>
            <a>暂停活动</a>
          </Popconfirm>
        ) : null,
        record.status == 2 ? (
          <Popconfirm key="top" title="确定要恢复活动吗?" onConfirm={() => handleStop(record)}>
            <a>恢复活动</a>
          </Popconfirm>
        ) : null,
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable<TableListItem, TableListPagination>
        actionRef={actionRef}
        search={false}
        rowKey="id"
        search={{
          labelWidth: 120,
        }}
        headerTitle={
          <Button
            type="primary"
            key="primary"
            onClick={() => {
              history.push('/activity/activity-detail');
            }}
          >
            <PlusOutlined /> 新增
          </Button>
        }
        request={async (
          // 第一个参数 params 查询表单和 params 参数的结合
          // 第一个参数中一定会有 pageSize 和  current ，这两个参数是 antd 的规范
          params,
          sorter,
          filter,
        ) => {
          console.log(params, formData, filter);
          // 这里需要返回一个 Promise,在返回之前你可以进行数据转化
          // 如果需要转化参数可以在这里进行修改
          let query = {
            eqQuery: {
              status: params.status,
            },
            pageNum: params.current,
            pageSize: params.pageSize,
          };
          const msg = await list(query);
          if (msg.code == 200) {
            return {
              data: msg.data.list,
              // success 请返回 true，
              // 不然 table 会停止解析数据，即使有数据
              success: msg.message,
              // 不传会使用 data 的长度，如果是分页一定要传
              total: msg.data.total,
            };
          } else {
          }
        }}
        columns={columns}
      />
    </PageContainer>
  );
};

export default TableList;
